using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Web;

namespace OF.Security.Utils
{
	public static class UriHelper
	{
		public static NameValueCollection GetUriParamsCollection(Uri url)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(url != null, "url", new object[0]);
			return UriHelper.GetUriParamsCollection(url.ToString());
		}

		public static NameValueCollection GetUriParamsCollection(string uriString)
		{
			return UriHelper.GetUriParamsCollection(uriString, true);
		}

		public static NameValueCollection GetUriParamsCollection(string uriString, bool urlDecode)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(uriString != null, "uriString", new object[0]);
			NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
			string bookmarkStringInUrl = UriHelper.GetBookmarkStringInUrl(uriString);
			if (bookmarkStringInUrl != string.Empty)
			{
				uriString = uriString.Remove(uriString.Length - bookmarkStringInUrl.Length, bookmarkStringInUrl.Length);
			}
			string text = uriString;
			int num = text.IndexOf("?");
			if (num >= 0)
			{
				text = text.Substring(num + 1);
			}
			if (text != string.Empty)
			{
				string[] array = text.Split(new char[]
				{
					'&'
				}, StringSplitOptions.RemoveEmptyEntries);
				for (int i = 0; i < array.Length; i++)
				{
					int num2 = array[i].IndexOf("=");
					string text2 = string.Empty;
					string text3 = string.Empty;
					if (num2 >= 0)
					{
						text2 = array[i].Substring(0, num2);
						text3 = array[i].Substring(num2 + 1);
					}
					if (!string.IsNullOrEmpty(text2))
					{
						if (urlDecode)
						{
							text2 = HttpUtility.UrlDecode(text2);
							text3 = HttpUtility.UrlDecode(text3);
						}
						UriHelper.AddValueToCollection(text2, text3, result);
					}
				}
			}
			return result;
		}

		public static string GetUrlWithSortedParams(Uri url)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(url != null, "url", new object[0]);
			return UriHelper.GetUrlWithSortedParams(url.ToString());
		}

		public static string GetUrlWithSortedParams(string uriString)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(uriString != null, "uriString", new object[0]);
			string text = string.Empty;
			string str = string.Empty;
			int num = uriString.IndexOf("?");
			if (num >= 0)
			{
				str = uriString.Substring(0, num) + "?";
				text = uriString.Substring(num + 1);
			}
			else
			{
				str = uriString;
			}
			StringBuilder stringBuilder = new StringBuilder(2048);
			if (text != string.Empty)
			{
				NameValueCollection uriParamsCollection = UriHelper.GetUriParamsCollection(text);
				string[] allKeys = uriParamsCollection.AllKeys;
				Array.Sort(allKeys, CaseInsensitiveComparer.Default);
				for (int i = 0; i < allKeys.Length; i++)
				{
					string text2 = allKeys[i];
					if (stringBuilder.Length > 0)
					{
						stringBuilder.Append("&");
					}
					stringBuilder.Append(HttpUtility.UrlEncode(text2));
					if (text2 != string.Empty)
					{
						stringBuilder.Append("=");
					}
					stringBuilder.Append(HttpUtility.UrlEncode(uriParamsCollection[text2]));
				}
			}
			return str + stringBuilder.ToString();
		}

		public static string CombineUrlParams(string uriString, Encoding encoding, params NameValueCollection[] requestParamsArray)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(uriString != null, "uriString", new object[0]);
			ExceptionHelper.FalseThrow<ArgumentNullException>(encoding != null, "encoding", new object[0]);
			ExceptionHelper.FalseThrow<ArgumentNullException>(requestParamsArray != null, "requestParamsArray", new object[0]);
			NameValueCollection nameValueCollection = UriHelper.MergeParamsCollection(requestParamsArray);
			StringBuilder stringBuilder = new StringBuilder(1024);
			string str = string.Empty;
			int num = uriString.IndexOf("?");
			if (num >= 0)
			{
				str = uriString.Substring(0, num);
			}
			else
			{
				str = uriString;
			}
			for (int i = 0; i < nameValueCollection.Count; i++)
			{
				if (i == 0)
				{
					stringBuilder.Append("?");
				}
				else
				{
					stringBuilder.Append("&");
				}
				stringBuilder.Append(HttpUtility.UrlEncode(nameValueCollection.Keys[i], encoding));
				stringBuilder.Append("=");
				stringBuilder.Append(HttpUtility.UrlEncode(nameValueCollection[i], encoding));
			}
			return str + stringBuilder.ToString();
		}

		public static string CombineUrlParams(string uriString, params NameValueCollection[] requestParamsArray)
		{
			return UriHelper.CombineUrlParams(uriString, Encoding.UTF8, requestParamsArray);
		}

		public static string GetBookmarkStringInUrl(string queryString)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(queryString != null, "queryString", new object[0]);
			int num = -1;
			for (int i = queryString.Length - 1; i >= 0; i--)
			{
				if (queryString[i] == '#')
				{
					num = i;
				}
				else if (queryString[i] == '&' || queryString[i] == '?')
				{
					break;
				}
			}
			string result = string.Empty;
			if (num >= 0)
			{
				result = queryString.Substring(num);
			}
			return result;
		}

		public static Uri ResolveUri(string uriString)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(uriString != null, "uriString", new object[0]);
			Uri uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
			if (!uri.IsAbsoluteUri && !string.IsNullOrEmpty(uriString))
			{
				if (EnvironmentHelper.Mode == InstanceMode.Web)
				{
					if (uriString[0] == '~')
					{
						HttpRequest request = HttpContext.Current.Request;
						string text = request.ApplicationPath + uriString.Substring(1);
						text = text.Replace("//", "/");
						uriString = request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped) + text;
						uri = new Uri(uriString);
					}
				}
			}
			return uri;
		}

		private static NameValueCollection MergeParamsCollection(NameValueCollection[] requestParams)
		{
			NameValueCollection nameValueCollection = new NameValueCollection();
			for (int i = 0; i < requestParams.Length; i++)
			{
				UriHelper.MergeTwoParamsCollection(nameValueCollection, requestParams[i]);
			}
			return nameValueCollection;
		}

		private static void MergeTwoParamsCollection(NameValueCollection target, NameValueCollection src)
		{
			foreach (string name in src.Keys)
			{
				if (target[name] == null)
				{
					target.Add(name, src[name]);
				}
			}
		}

		private static void AddValueToCollection(string paramName, string paramValue, NameValueCollection result)
		{
			string text = result[paramName];
			if (text == null)
			{
				result.Add(paramName, paramValue);
			}
			else
			{
				string text2 = text;
				if (text.Length > 0)
				{
					text2 += ",";
				}
				text2 += paramValue;
				result[paramName] = text2;
			}
		}
	}
}
